<?php

namespace App\Admin\Controllers;

use App\Models\Attachment;
use Dcat\Admin\Controllers\AdminController;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Traits\HasUploadedFile;
use Illuminate\Http\Request;

class AttachmentController extends AdminController {
	use HasUploadedFile;
	/**
	 * 文件上传
	 * @param  $file   [文件表单名file或者_file_]
	 * @return [json]           [description]
	 */
	public function handle(Request $request) {
		$driver = env("FILESYSTEM_DRIVER", 'local');
		$disk = $this->disk($driver);
		// 判断是否是删除文件请求
		if ($this->isDeleteRequest()) {
			// 删除文件并响应
			return $this->deleteFileAndResponse($disk);
		}
		if ($request->hasFile('file')) {
			$file = $request->file("file");
		} else {
			$file = $request->file("_file_");
		}
		if (empty($file)) {
			return $this->responseErrorMessage('未检测到文件');
		}
		$md5 = md5(md5_file($file) . time() . mt_rand(1000, 9999));

		$model = new Attachment();
		/**暂时取消 同一文件不覆盖
			$exist = $model->where("md5", $md5)->first();
			if (!empty($exist)) {
				return $this->responseUploaded($exist->path, $disk->url($exist->path));
			}
		*/
		// 获取上传的字段名称
		$pic = ['jpg', 'jpeg', 'gif', 'bmp', 'png', "ico", 'svg'];
		$dir = "file";
		if (in_array(strtolower($file->extension()), $pic)) {
			$dir = "image";
		}
		$type = $dir;
		$newName = date("Ym") . "/" . $md5 . "." . $file->getClientOriginalExtension();
		try {
			$result = $disk->putFileAs($dir . "s", $file, $newName);
			$path = $dir . "s/" . $newName;
			if ($result) {
				$attachment = new Attachment([
					'model' => $request->input("module", "default"),
					'path' => $path,
					'type' => $type,
					'md5' => $md5,
					'name' => $file->getClientOriginalName(),
					'driver' => $driver,
					'create_at' => date("Y-m-d H:i:s"),
					'updated_at' => date("Y-m-d H:i:s"),
				]);
				$attachment->save();
			}

			return $result
			? $this->responseUploaded($path, $disk->url($path))
			: $this->responseErrorMessage('文件上传失败');
		} catch (\Excetion $e) {
			$this->responseErrorMessage('文件上传失败, ' . $e->getMessage());
		}

	}

	/**
	 *
	 * @return Grid
	 */
	protected function grid() {
		return Grid::make(new Attachment(), function (Grid $grid) {
			$grid->disableRefreshButton();
			$grid->disableFilterButton();
			$grid->quickSearch(['name', 'path'])->placeholder('搜索文件名');
			$grid->id->sortable();
			$grid->name("原文件名")->width("20%")->style("word-break:break-all;");
			$grid->column('path0', '保存路径')->style("word-break:break-all;")->display(function () {
				return $this->path;
			});
			$grid->path("预览")->image(picture_server(), 50, 50);

			$grid->created_at->sortable();

			$grid->filter(function (Grid\Filter $filter) {
				$filter->equal('id');

			});
		});
	}

	/**
	 *
	 * @return Show
	 */
	protected function detail($id) {
		return Show::make($id, new Attachment(), function (Show $show) {
			$show->id;
			$show->module;
			$show->type;
			$show->driver;
			$show->name;
			$show->path;
			$show->created_at;
			$show->updated_at;
		});
	}

	/**
	 * Make a form builder.
	 *
	 * @return Form
	 */
	protected function form() {
		return Form::make(new Attachment(), function (Form $form) {
			$form->display('id');
			$form->text('module');
			$form->text('type');
			$form->text('driver');
			$form->text('name');
			$form->text('path');

			$form->display('created_at');
			$form->display('updated_at');
		});
	}
}
